/*DONE* Table [dbo].[<CompanyName>$Transfer Shipment Line$437dbf0e-84ff-417a-965d-ed2bb9650972]  1/2  Script Date: 14.1.2021 - JI ******/
/*Add prestaging table for one specific company*/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

DECLARE @CreateTable as VARCHAR(MAX)
DECLARE @Company as VARCHAR(100)
DECLARE @CompanyID as INT

SET @CompanyID = X --The X is the value that needs to be replaced with the [Company] ID from LSInsight$Companies
SET @Company = (SELECT LSInsight$Companies.[CompanyPrefix] from dbo.LSInsight$Companies WHERE [Company] = @CompanyID)

IF @Company IS NOT NULL
BEGIN

DECLARE @DropTable as VARCHAR(MAX)
SET @DropTable = 'DROP TABLE IF EXISTS [dbo].[' + @Company + '$Transfer Shipment Line$437dbf0e-84ff-417a-965d-ed2bb9650972]'
EXEC (@DropTable)

SET @CreateTable = 'CREATE TABLE [dbo].[' + @Company + '$Transfer Shipment Line$437dbf0e-84ff-417a-965d-ed2bb9650972](
	[timestamp] [timestamp] NOT NULL,
	[Document No_] [nvarchar](20) NOT NULL,
	[Line No_] [int] NOT NULL,
	[Item No_] [nvarchar](20) NOT NULL,
	[Quantity] [decimal](38, 20) NOT NULL,
	[Unit of Measure] [nvarchar](50) NOT NULL,
	[Description] [nvarchar](100) NOT NULL,
	[Shortcut Dimension 1 Code] [nvarchar](20) NOT NULL,
	[Shortcut Dimension 2 Code] [nvarchar](20) NOT NULL,
	[Gen_ Prod_ Posting Group] [nvarchar](20) NOT NULL,
	[Inventory Posting Group] [nvarchar](20) NOT NULL,
	[Quantity (Base)] [decimal](38, 20) NOT NULL,
	[Qty_ per Unit of Measure] [decimal](38, 20) NOT NULL,
	[Unit of Measure Code] [nvarchar](10) NOT NULL,
	[Gross Weight] [decimal](38, 20) NOT NULL,
	[Net Weight] [decimal](38, 20) NOT NULL,
	[Unit Volume] [decimal](38, 20) NOT NULL,
	[Variant Code] [nvarchar](10) NOT NULL,
	[Units per Parcel] [decimal](38, 20) NOT NULL,
	[Description 2] [nvarchar](50) NOT NULL,
	[Transfer Order No_] [nvarchar](20) NOT NULL,
	[Shipment Date] [datetime] NOT NULL,
	[Shipping Agent Code] [nvarchar](10) NOT NULL,
	[Shipping Agent Service Code] [nvarchar](10) NOT NULL,
	[In-Transit Code] [nvarchar](10) NOT NULL,
	[Transfer-from Code] [nvarchar](10) NOT NULL,
	[Transfer-to Code] [nvarchar](10) NOT NULL,
	[Item Shpt_ Entry No_] [int] NOT NULL,
	[Shipping Time] [varchar](32) NOT NULL,
	[Dimension Set ID] [int] NOT NULL,
	[Item Category Code] [nvarchar](20) NOT NULL,
	[Product Group Code] [nvarchar](10) NOT NULL,
	[Transfer-from Bin Code] [nvarchar](20) NOT NULL,
	[$systemId] [uniqueidentifier] NOT NULL,
	[$systemCreatedAt] [datetime] NOT NULL,
	[$systemCreatedBy] [uniqueidentifier] NOT NULL,
	[$systemModifiedAt] [datetime] NOT NULL,
	[$systemModifiedBy] [uniqueidentifier] NOT NULL,
 CONSTRAINT [' + @Company + '$Transfer Shipment Line$437dbf0e-84ff-417a-965d-ed2bb9650972$Key1] PRIMARY KEY CLUSTERED 
(
	[Document No_] ASC,
	[Line No_] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
 CONSTRAINT [' + @Company + '$Transfer Shipment Line$437dbf0e-84ff-417a-965d-ed2bb9650972$$systemId] UNIQUE NONCLUSTERED 
(
	[$systemId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]'

EXEC(@CreateTable)

END